package com.timmy.google_play.fragment;

import android.os.SystemClock;
import android.view.View;
import android.widget.ListView;

import com.timmy.google_play.base.BaseFragment;
import com.timmy.google_play.base.ItemAdapter;
import com.timmy.google_play.base.ListViewFactory;
import com.timmy.google_play.base.LoadingPager;
import com.timmy.google_play.bean.HomeBean;
import com.timmy.google_play.bean.ItemBean;
import com.timmy.google_play.holder.HomePictureHolder;
import com.timmy.google_play.holder.ItemHolder;
import com.timmy.google_play.manager.DownLoadInfo;
import com.timmy.google_play.manager.DownLoadManager;
import com.timmy.google_play.protocol.HomeProtocol;

import java.util.List;

/**
 * Created by Timmy on 2016/12/7.
 * <p>
 * 首页
 */

public class HomeFragment extends BaseFragment {

    private ListView       mListView;
    private List<String>   mDatas;
    private List<ItemBean> mItemBeanList;
    private List<String>   mPictureList;
    private HomeProtocol   mProtocol;
    private MyHomeAdapter  mAdapter;

    @Override
    protected LoadingPager.LoadedResult initData() {

        try {

            mProtocol = new HomeProtocol();
            HomeBean homeBean = null;
            homeBean = mProtocol.loadData(0);

            LoadingPager.LoadedResult state = checkResData(homeBean);

            if (state != LoadingPager.LoadedResult.SUCCESS) {
                return state;
            }

            state = checkResData(homeBean.list);

            if (state != LoadingPager.LoadedResult.SUCCESS) {
                return state;
            }

            //到这里表明数据请求下来,保存数据为成员变量
            mItemBeanList = homeBean.list;
            mPictureList = homeBean.picture;

            return state;

        } catch (Exception e1) {
            e1.printStackTrace();

            return LoadingPager.LoadedResult.ERROR;
        }
    }

    @Override
    protected View initSuccessView() {

        mListView = ListViewFactory.createListView();

        //首页Viewpager 轮播

        HomePictureHolder homePictureHolder = new HomePictureHolder();
        homePictureHolder.setDataAndRefreshHolderView(mPictureList);

        mListView.addHeaderView(homePictureHolder.holdView);
        mAdapter = new MyHomeAdapter(mListView, mItemBeanList);
        mListView.setAdapter(mAdapter);

        return mListView;
    }

    class MyHomeAdapter extends ItemAdapter {

        public MyHomeAdapter(ListView listView, List<ItemBean> datas) {
            super(listView, datas);
        }


        /**
         * 子线程中具体如何加载更多数据
         */
        @Override
        public List<ItemBean> onLoadMoreData() throws Exception {

            //加载更多的时间
            SystemClock.sleep(500);

            HomeBean homeBean = mProtocol.loadData(mDatas.size());

            if (homeBean != null) {
                return homeBean.list;
            }
            return null;
        }
    }

    @Override
    public void onResume() {
        //添加观察者
        if (mAdapter != null) {

            List<ItemHolder> itemHolders = mAdapter.getItemHolders();

            for (ItemHolder itemHolder : itemHolders) {
                DownLoadManager.getInstance().addObserver(itemHolder);

                //手动发布最新的状态信息
                //1.得到最新的downloadinfo信息
                DownLoadInfo downLoadInfo = DownLoadManager.getInstance().getDownLoadInfo(itemHolder.mData);

                //2.发布最新消息
                DownLoadManager.getInstance().notifyObserver(downLoadInfo);

            }
        }


        super.onResume();
    }

    @Override
    public void onPause() {
        //移除观察者
        if (mAdapter != null) {

            List<ItemHolder> itemHolders = mAdapter.getItemHolders();

            for (ItemHolder itemHolder : itemHolders) {
                DownLoadManager.getInstance().deleteObserver(itemHolder);
            }
        }

        super.onPause();
    }
}
